package com.ay.smartpark.sdk.controller;


import com.ay.smartpark.sdk.common.base.ResponseBean;
import com.ay.smartpark.sdk.common.utils.DateUtils;
import com.ay.smartpark.sdk.common.utils.ServiceUtils;
import com.ay.smartpark.sdk.service.YanLianJiHuaYuFangAnRpcService;
import com.qycloud.SDK.server.client.GenerateTableIdClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yanLianJiHuaYuFangAn")
public class YanLianJiHuaYuFangAnController {

    private static final Logger logger = LoggerFactory.getLogger(YanLianJiHuaYuFangAnController.class);

    @Resource
    private GenerateTableIdClient generateTableIdClient;

    private YanLianJiHuaYuFangAnRpcService yanLianJiHuaYuFangAnRpcService;

    /**
     * 根据entId查找service实现类
     *
     * @ModelAttribute 在此controller每个方法执行前被执行
     */
    @ModelAttribute
    private void injectionServiceImpl() {
        if (yanLianJiHuaYuFangAnRpcService == null) {
            yanLianJiHuaYuFangAnRpcService = ServiceUtils.getServiceImpl("YanLianJiHuaYuFangAnRpcServiceImpl", YanLianJiHuaYuFangAnRpcService.class);
        }
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/selectMapList")
    public ResponseBean selectMapList() {
        try {
            Map<String, Object> selectMap = new HashMap<>();
            String tableId = generateTableIdClient.generateDataPoolTableId("zhonghuawuliugangxin", "yanlianjihuayufangan");
            List<String> fields = new ArrayList<>();
            fields.add("id");
            fields.add("yanlianjihuamingchen");
            fields.add("owner");
            fields.add("modifier");
            fields.add("last_modified");
            fields.add("modified_method");
            fields.add("app_key");
            fields.add("app_extend_key");
            Map<String, Object> condition = new HashMap<>();
            condition.put("disable", "0");
            condition.put("owner", "admin");

            selectMap.put("tableId", tableId);
            selectMap.put("fields", fields);
            selectMap.put("condition", condition);
            List<Map<String, Object>> mapList = yanLianJiHuaYuFangAnRpcService.selectMapList(selectMap);
            return ResponseBean.success(mapList);
        } catch (Exception e) {
            logger.error("selectMapList error:", e);
            return ResponseBean.fail("");
        }
    }

    /**
     * 增加
     *
     * @return
     */
    @GetMapping("/insertMap")
    public ResponseBean insertMap() {
        try {
            Map<String, Object> insertMap = new HashMap<>();
            String tableId = generateTableIdClient.generateDataPoolTableId("zhonghuawuliugangxin", "yanlianjihuayufangan");
            insertMap.put("tableId", tableId);
            insertMap.put("yanlianjihuamingchen", "测试insert演练名称1");
            insertMap.put("owner", "admin1");
            insertMap.put("modifier", "admin1");
            insertMap.put("last_modified", DateUtils.getTime());
            insertMap.put("modified_method", "workflow/yanLianJiHuaYuFangAn");
            insertMap.put("app_key", "workflow/0");
            insertMap.put("app_extend_key", "WorkNode_0");
            insertMap.put("disable", "0");
            int count = yanLianJiHuaYuFangAnRpcService.insertMap(insertMap);
            return ResponseBean.success(count);
        } catch (Exception e) {
            logger.error("insertMap error:", e);
            return ResponseBean.fail("");
        }
    }

    /**
     * 更新
     *
     * @return
     */
    @GetMapping("/updateMap")
    public ResponseBean updateMap() {
        try {
            Map<String, Object> updateMap = new HashMap<>();
            String tableId = generateTableIdClient.generateDataPoolTableId("zhonghuawuliugangxin", "yanlianjihuayufangan");
            updateMap.put("tableId", tableId);
            updateMap.put("yanlianjihuamingchen", "测试insert演练名称3");
            updateMap.put("owner", "admin3");
            updateMap.put("modifier", "admin3");
            updateMap.put("last_modified", DateUtils.getTime());
            updateMap.put("modified_method", "workflow/yanLianJiHuaYuFangAn");
            updateMap.put("app_key", "workflow/0");
            updateMap.put("app_extend_key", "WorkNode_0");
            updateMap.put("disable", "0");

            Map<String, Object> condition = new HashMap<>();
            condition.put("owner", "admin1");
            updateMap.put("condition", condition);

            int count = yanLianJiHuaYuFangAnRpcService.updateMap(updateMap);
            return ResponseBean.success(count);
        } catch (Exception e) {
            logger.error("updateMap error:", e);
            return ResponseBean.fail("");
        }
    }

    /**
     * 删除
     *
     * @return
     */
    @GetMapping("/deleteMap")
    public ResponseBean deleteMap() {
        try {
            Map<String, Object> deleteMap = new HashMap<>();
            String tableId = generateTableIdClient.generateDataPoolTableId("zhonghuawuliugangxin", "yanlianjihuayufangan");
            deleteMap.put("tableId", tableId);

            Map<String, Object> condition = new HashMap<>();
            condition.put("owner", "admin3");
            deleteMap.put("condition", condition);

            int count = yanLianJiHuaYuFangAnRpcService.deleteMap(deleteMap);
            return ResponseBean.success(count);
        } catch (Exception e) {
            logger.error("deleteMap error:", e);
            return ResponseBean.fail("");
        }
    }


}
